/*
 * > /*
>  * INSANE - Interactive Structural Analysis Environment
>  *
>  * Copyright (C) 2003-2004
>  * Universidade Federal de Minas Gerais
>  * Escola de Engenharia
>  * Departamento de Engenharia de Estruturas
>  *
>  * Author's email :     insane@dees.ufmg.br
>  * Author's Website :   http://www.dees.ufmg.br/insane
>  *
>  * This program is free software; you can redistribute it and/or
>  * modify it under the terms of the GNU General Public License
>  * as published by the Free Software Foundation; either version 2
>  * of the License, or any later version.
>  *
>  * This program is distributed in the hope that it will be useful,
>  * but WITHOUT ANY WARRANTY; without even the implied warranty of
>  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
>  * GNU General Public License for more details.
>  *
>  * You should have received a copy of the GNU General Public License
>  * along with this program; if not, write to the Free Software
>  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-
> 1307, USA.
 *
 */

package br.ufmg.dees.insane.ui.rich.postp.postp_view.dialog;

import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.WindowConstants;

import br.ufmg.dees.insane.commons.view.ViewState;
import br.ufmg.dees.insane.model.postp.PostpModel;
import br.ufmg.dees.insane.model.postp.StepPostp;
import br.ufmg.dees.insane.ui.rich.postp.postp_controller.PostpController;
import br.ufmg.dees.insane.ui.rich.postp.postp_view.DesktopPostp;
import br.ufmg.dees.insane.ui.rich.postp.postp_view.InterfacePostp;
import br.ufmg.dees.insane.ui.rich.postp.postp_view.PostpViewState;

public class NodalContoursSelectionDialog extends JFrame {
		
	/**   */
	private static final long serialVersionUID = 1L;
	
	private DesktopPostp desktop;
	private InterfacePostp intFrame;
	private PostpViewState state;
	private PostpModel postpModel;
	private StepPostp stepPostp;
	private JList panelList;
	private JPanel contentPane; 
    private String[] precisionStr = {"0", "0.0", "0.00", "0.000", "0.0000", "0.00000", "0.000000", "0.0000000", "0.00000000"
    								 ,"0.000000000","0.0000000000","0.00000000000","0.000000000000"};
    private JComboBox precision = new JComboBox(precisionStr);
	private JButton copyButton1;
	private JButton setColorsButton;
	private JButton viewButton;
	private JButton okButton;
	private JButton cancelButton;
	private JTextField textField1;
	private JCheckBox openNewWindowCheckBox = new JCheckBox("Open In a New Window");
	private String[] keysList;
	private JScrollPane scroll;
	
	private NodalContoursSelectionDialog thisDlg;
	
	public NodalContoursSelectionDialog(String[] keys, ViewState vs){
		this.keysList = new String[keys.length+1];
		for (int i = 0; i < this.keysList.length-1; i++) {
			this.keysList[i] = keys[i];
		}
		this.keysList[keysList.length-1] = ""; 
		this.state = (PostpViewState)vs;
		new Thread(new Runnable(){
			public void run() {
				init();
			}
		}).start();
	}
	
	public void init(){
		thisDlg = this;
		this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		this.setTitle("Contours Range"+" - "+this.intFrame.getPostpModel().getLabel());
		Image ico = Toolkit.getDefaultToolkit().getImage(ClassLoader.getSystemResource("br/ufmg/dees/insane/images/Insane64.gif"));
		this.setIconImage(ico);
		//setting the createdSequencesPane
		JPanel listPane = new JPanel();
		listPane.setSize(new Dimension(500,700));
		listPane.setBorder(BorderFactory.createCompoundBorder(
        		BorderFactory.createTitledBorder("Value Selection"),
        		BorderFactory.createEmptyBorder(2,2,2,2)));
		listPane.setLayout(new BoxLayout(listPane,BoxLayout.Y_AXIS)); 
		panelList = new JList(keysList);
		panelList.setBackground(Color.BLACK);
		panelList.setForeground(Color.GREEN);
		panelList.setVisibleRowCount(5);
		panelList.setFixedCellHeight(20);
		panelList.setFixedCellWidth(300);
		panelList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		panelList.setAutoscrolls(true);
		panelList.setFixedCellHeight(20);
		scroll = new JScrollPane(panelList);
		listPane.add(scroll);
		scroll.setViewportView(panelList); 
		
		copyButton1 = new JButton(">> Copy >>");
		
		setColorsButton = new JButton("Palette");
		viewButton = new JButton("View");
		okButton = new JButton("    OK    ");
		cancelButton = new JButton("Cancel");
		
		textField1 = new JTextField();
        textField1.setText("");
		textField1.setBorder(BorderFactory.createCompoundBorder(
        		BorderFactory.createTitledBorder(""),
        		BorderFactory.createEmptyBorder(2,2,2,2)));
		textField1.setBackground(Color.BLACK);
		textField1.setForeground(Color.GREEN);
		textField1.setEditable(false);
		textField1.setPreferredSize(new Dimension(150,20));
	    
		precision.setSelectedIndex(state.getFracDigits());
		
		contentPane = (JPanel)this.getContentPane(); 
		
	    copyButton1.addActionListener(
				new ActionListener(){
					public void actionPerformed(ActionEvent event){
						textField1.setText(addText(panelList));
					}
				});
	    
	    setColorsButton.addActionListener(
				new ActionListener(){
					public void actionPerformed(ActionEvent event){
						//ColorsPaleteChoserDialog dlg = new ColorsPaleteChoserDialog((InterfacePostp)intFrame,state);
						new ColorsPalete32ChooserDialog((InterfacePostp)intFrame,state);
					}
				});
	    
	    viewButton.addActionListener(
				new ActionListener(){
					public void actionPerformed(ActionEvent event){
                        if(textField1.getText().equals("")){
                            String message = "You must select a value!"; 
                            JOptionPane.showMessageDialog(thisDlg, message, "Warning", JOptionPane.INFORMATION_MESSAGE);
                        }else{
                            view(event);
                        }
					}
				});
	    
	    okButton.addActionListener(
				new ActionListener(){
					public void actionPerformed(ActionEvent event){
                        if(textField1.getText().equals("")){
                            String message = "You must select a value!"; 
                            JOptionPane.showMessageDialog(thisDlg, message, "Warning", JOptionPane.INFORMATION_MESSAGE);
                        }else{
                            approvedPerform();
                            close(event);
                        }
                        
					}
				});
	    
	    cancelButton.addActionListener(
				new ActionListener(){
					public void actionPerformed(ActionEvent event){
						close(event);
					}
				});
	  
 		// Adding components in the contentPane 
 		contentPane.setLayout(null); 
 		addComponent(contentPane, listPane, 5,5,240,240); 
 		addComponent(contentPane, new JLabel("Value:"), 365,15,50,25);
 		addComponent(contentPane, copyButton1, 255,45,100,25);
 		addComponent(contentPane, textField1, 365,41,100,33);
 		addComponent(contentPane, setColorsButton, 255,100,100,25);
 		addComponent(contentPane, viewButton, 365,100,100,25);
 		addComponent(contentPane, new JLabel("Precision:"), 255,140,75,25);
 		addComponent(contentPane, precision, 350,140,120,25);
 		addComponent(contentPane, openNewWindowCheckBox, 275,190,200,20);
 		addComponent(contentPane,okButton, 255,220,100,25);
 		addComponent(contentPane,cancelButton, 365,220,100,25);
	    
	    this.setSize(485,285);
	    this.setLocation(200,200);
	    this.setResizable(false);
	    this.setAlwaysOnTop(true);
		this.setVisible(true);
		this.repaint();
		
	}
	
	public String addText(JList list){
		String text="";
		for(int i=0;i<list.getSelectedValues().length;i++){
			text+=((String)list.getSelectedValues()[i]);
		}
		return text;
	}
	
	/** Add Component Without a Layout Manager (Absolute Positioning) */ 
 	private void addComponent(Container container,Component c,int x,int y,int width,int height) 
 	{ 
 		c.setBounds(x,y,width,height); 
 		container.add(c); 
 	} 

	public void close(ActionEvent event){
		this.dispose();
	}
	
	public void view(ActionEvent event){
        this.state.setDeformedOn(true);
        if(this.openNewWindowCheckBox.isSelected()){
            InterfacePostp intFrame = new InterfacePostp();
            intFrame.validate();
            intFrame.setPostpModel(this.postpModel);
            intFrame.setModel(this.stepPostp);
            intFrame.setTitle(postpModel.getLabel());
            ((DesktopPostp)this.desktop).addInternalFrame(intFrame);
            intFrame.toFront();
            this.state = (PostpViewState)((InterfacePostp)intFrame).getView().getViewState();
            this.state.getEdgeViewState().setLineColor(Color.WHITE);
            this.state.getEdgeViewState().setLineWindth(0.5f);
            this.state.getElementsPlotContoursViewState().setContourOnOff(false);
            this.state.getElementsPlotContoursViewState().setDiscontinuosContourOnOff(false);
            if(this.state.getNodalsPlotContoursViewState().isContourMeshStateOnOff()){
            	this.state.getNodalsPlotContoursViewState().setContourOnOff(false);
            	this.state.getNodalsPlotContoursViewState().setContourMeshStateOnOff(true);
            }else{
            	this.state.getNodalsPlotContoursViewState().setContourOnOff(true);
            	this.state.getNodalsPlotContoursViewState().setContourMeshStateOnOff(false);
            }
            this.state.getNodalsPlotContoursViewState().setNodalsContoursComputed(true);
            this.state.getNodalsPlotContoursViewState().setKeyValue(this.textField1.getText());
            this.state.setFracDigits(this.precision.getSelectedIndex());
            this.state.getScaleColorsViewState().setScaleContoursOnOff(true);
            ((PostpController)((InterfacePostp)this.intFrame).getView().getController()).width = 2; 
            (intFrame).getView().getDrawingArea().repaint();    
        }else{
            this.state = (PostpViewState)((InterfacePostp)intFrame).getView().getViewState();
            this.state.getEdgeViewState().setLineColor(Color.WHITE);
            this.state.getEdgeViewState().setLineWindth(0.5f);
            this.state.getElementsPlotContoursViewState().setContourOnOff(false);
            this.state.getElementsPlotContoursViewState().setDiscontinuosContourOnOff(false);
            if(this.state.getNodalsPlotContoursViewState().isContourMeshStateOnOff()){
            	this.state.getNodalsPlotContoursViewState().setContourOnOff(false);
            	this.state.getNodalsPlotContoursViewState().setContourMeshStateOnOff(true);
            }else{
            	this.state.getNodalsPlotContoursViewState().setContourOnOff(true);
            	this.state.getNodalsPlotContoursViewState().setContourMeshStateOnOff(false);
            }
            this.state.getNodalsPlotContoursViewState().setNodalsContoursComputed(true);
            this.state.getNodalsPlotContoursViewState().setKeyValue(this.textField1.getText());
            this.state.setFracDigits(this.precision.getSelectedIndex());
            this.state.getScaleColorsViewState().setScaleContoursOnOff(true);
            ((PostpController)((InterfacePostp)this.intFrame).getView().getController()).width = 2; 
            ((InterfacePostp)((DesktopPostp)this.desktop).getDesktop().getSelectedFrame()).getView().getDrawingArea().repaint();
        }
    }
	
	public void showErrMessage(ActionEvent event){
		JOptionPane.showMessageDialog(this, "Set the X and Y fields at least!", "Error Message", JOptionPane.ERROR_MESSAGE);
	}
	
	public void approvedPerform(){
		this.state.setDeformedOn(true);
		if(this.openNewWindowCheckBox.isSelected()){
            InterfacePostp intFrame = new InterfacePostp();
            intFrame.validate();
            intFrame.setPostpModel(this.postpModel);
            intFrame.setModel(this.stepPostp);
            intFrame.setTitle(postpModel.getLabel());
            ((DesktopPostp)this.desktop).addInternalFrame(intFrame);
            intFrame.toFront();
            this.state = (PostpViewState)((InterfacePostp)intFrame).getView().getViewState();
            this.state.getEdgeViewState().setLineColor(Color.WHITE);
            this.state.getEdgeViewState().setLineWindth(0.5f);
            this.state.getElementsPlotContoursViewState().setContourOnOff(false);
            this.state.getElementsPlotContoursViewState().setDiscontinuosContourOnOff(false);
            if(this.state.getNodalsPlotContoursViewState().isContourMeshStateOnOff()){
            	this.state.getNodalsPlotContoursViewState().setContourOnOff(false);
            	this.state.getNodalsPlotContoursViewState().setContourMeshStateOnOff(true);
            }else if(!this.state.getNodalsPlotContoursViewState().isContourMeshStateOnOff()){
            	this.state.getNodalsPlotContoursViewState().setContourOnOff(true);
            	this.state.getNodalsPlotContoursViewState().setContourMeshStateOnOff(false);
            }
            this.state.getNodalsPlotContoursViewState().setNodalsContoursComputed(true);
            this.state.getNodalsPlotContoursViewState().setKeyValue(this.textField1.getText());
            this.state.setFracDigits(this.precision.getSelectedIndex());
            this.state.getScaleColorsViewState().setScaleContoursOnOff(true);
            ((PostpController)((InterfacePostp)this.intFrame).getView().getController()).width = 2; 
            (intFrame).getView().getDrawingArea().repaint();
		}else{
            this.state = (PostpViewState)((InterfacePostp)intFrame).getView().getViewState();
            this.state.getEdgeViewState().setLineColor(Color.WHITE);
            this.state.getEdgeViewState().setLineWindth(0.5f);
            this.state.getElementsPlotContoursViewState().setContourOnOff(false);
            this.state.getElementsPlotContoursViewState().setDiscontinuosContourOnOff(false);
            if(this.state.getNodalsPlotContoursViewState().isContourMeshStateOnOff()){
            	this.state.getNodalsPlotContoursViewState().setContourOnOff(false);
            	this.state.getNodalsPlotContoursViewState().setContourMeshStateOnOff(true);
            }else if(!this.state.getNodalsPlotContoursViewState().isContourMeshStateOnOff()){
            	this.state.getNodalsPlotContoursViewState().setContourOnOff(true);
            	this.state.getNodalsPlotContoursViewState().setContourMeshStateOnOff(false);
            }
            this.state.getNodalsPlotContoursViewState().setNodalsContoursComputed(true);
            this.state.getNodalsPlotContoursViewState().setKeyValue(this.textField1.getText());
            this.state.setFracDigits(this.precision.getSelectedIndex());
            this.state.getScaleColorsViewState().setScaleContoursOnOff(true);
            ((PostpController)((InterfacePostp)this.intFrame).getView().getController()).width = 2; 
            ((InterfacePostp)((DesktopPostp)this.desktop).getDesktop().getSelectedFrame()).getView().getDrawingArea().repaint();
        }
	}
	
	/**
	 * The method return the intFrame.
	 * @return Returns The intFrame.
	 */
	public JInternalFrame getIntFrame() {
	 return intFrame;
	}

	/**
	 * The method set the field intFrame from the parameter intFrame.
	 * @param intFrame The intFrame to set.
	 */
	public void setIntFrame(InterfacePostp intFrame) {
		this.intFrame = intFrame;
	}

	/**
	 * The method return the parent.
	 * @return Returns The parent.
	 */
	public DesktopPostp getDesktop() {
		return desktop;
	}

	/**
	 * The method set the field desktop from the parameter desktop.
	 * @param desktop The desktop to set.
	 */
	public void setDesktop(DesktopPostp desktop) {
		this.desktop = desktop;
		this.intFrame = (InterfacePostp)this.desktop.getDesktop().getSelectedFrame();
	}
	
	/**
	 * The method return the postpModel.
	 * @return Returns The postpModel.
	 */
	public PostpModel getPostpModel() {
		return postpModel;
	}

	/**
	 * The method set the field postpModel from the parameter postpModel.
	 * @param postpModel The postpModel to set.
	 */
	public void setPostpModel(PostpModel postpModel) {
		this.postpModel = postpModel;
	}

	/**
	 * The method return the stePostp.
	 * @return Returns The stePostp.
	 */
	public StepPostp getStePostp() {
		return stepPostp;
	}

	/**
	 * The method set the field stePostp from the parameter stePostp.
	 * @param stePostp The stePostp to set.
	 */
	public void setStePostp(StepPostp stePostp) {
		this.stepPostp = stePostp;
	}
	
//*********************************************************************************************	
}//end of this class

	


